package controllers;

import entity.User;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import jdbc.Function;
import util.AuxiliaryCalculationTools;
import view.LoginInterface;

/**
 * @author zjw
 * @date 2020/1/8 13:31
 */
public class RegisterInterfaceControllers {
    private Stage primaryStage;
    private TextField userNameTextField;
    private PasswordField passwordField;
    private PasswordField passwordAgainField;
    private Button registerButton;
    private Button returnButton;

    public RegisterInterfaceControllers(Stage primaryStage, TextField userNameTextField, PasswordField passwordField, PasswordField passwordAgainField) {
        this.primaryStage = primaryStage;
        this.userNameTextField = userNameTextField;
        this.passwordField = passwordField;
        this.passwordAgainField = passwordAgainField;

        registerButton = new Button("注册");
        returnButton = new Button("返回");
    }

    public Button getRegisterButton() {
        registerButton.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                while(true) {
                    Alert inconsistentAlert = new Alert(Alert.AlertType.ERROR);
                    if(!passwordField.getText().equals(passwordAgainField.getText()) || passwordField.getText().equals("")){
                        inconsistentAlert.setTitle("ERROR");
                        inconsistentAlert.setHeaderText("密码不一致或密码框中为空！");
                        inconsistentAlert.setContentText("两次密码不一致，需要重置！");
                        passwordField.clear();
                        passwordAgainField.clear();
                        inconsistentAlert.showAndWait();
                        break;
                    }
                    String newId = AuxiliaryCalculationTools.automaticallyGenerateIdNumber();
                    User user = new User(newId, passwordField.getText(), userNameTextField.getText());
                    Function function = new Function();
                    function.insert(user);
                    Alert newIdAlert = new Alert(Alert.AlertType.INFORMATION);
                    newIdAlert.setTitle("你的新号码！");
                    newIdAlert.setHeaderText("你获得了一个新号码，快记下来！否则你号没了！！！");
                    newIdAlert.setContentText("你的新号码是：" + newId);
                    newIdAlert.showAndWait();
                    primaryStage.close();
                    LoginInterface loginInterface = new LoginInterface();
                    loginInterface.start(new Stage());
                    break;
                }
            }
        });
        return registerButton;
    }

    public Button getReturnButton() {
        returnButton.setOnAction(event -> {
            primaryStage.close();
            new LoginInterface().start(new Stage());
        });
        return returnButton;
    }
}
